package cn.zlg.www.view;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;

import com.sany.canwifi.R;
public class PortAlertDialog extends AlertDialog.Builder {
	public static final int PORT1 = 0;
	public static final int PORT2 = 1;
	public static final int PORT3 = 3;
	public static final int PORT4 = 4;
	public static final int PORT5 = 5;
	private NumberPicker mPort1, mPort2, mPort3, mPort4, mPort5;
	private LayoutInflater mLayoutInflater;

	public PortAlertDialog(Context context, LayoutInflater layoutInflater) {
		super(context);
		// TODO Auto-generated constructor stub
		mLayoutInflater = layoutInflater;
		init();
	}

	public void setRange(int nWhich, int nMin, int nMax) {
		switch (nWhich) {
		case PORT1:
			if (mPort1 != null) {
				mPort1.setMinValue(nMin);
				mPort1.setMaxValue(nMax);
			}
			break;
		case PORT2:
			if (mPort2 != null) {
				mPort2.setMinValue(nMin);
				mPort2.setMaxValue(nMax);
			}
			break;
		case PORT3:
			if (mPort3 != null) {
				mPort3.setMinValue(nMin);
				mPort3.setMaxValue(nMax);
			}
			break;
		case PORT4:
			if (mPort4 != null) {
				mPort4.setMinValue(nMin);
				mPort4.setMaxValue(nMax);
			}
			break;
		case PORT5:
			if (mPort5 != null) {
				mPort5.setMinValue(nMin);
				mPort5.setMaxValue(nMax);
			}
		default:
			break;
		}
	}

	public void setPort(int nPort) {
		String strPortString = "" + nPort;
		int nLen = 5 - strPortString.length();
		for (int i = 0; i < nLen; i++) {
			strPortString = "0" + strPortString;
		}
		if (mPort1 != null) {
			mPort1.setValue(strPortString.charAt(0) - '0');
		}
		if (mPort2 != null) {
			mPort2.setValue(strPortString.charAt(1) - '0');
		}
		if (mPort3 != null) {
			mPort3.setValue(strPortString.charAt(2) - '0');
		}
		if (mPort4 != null) {
			mPort4.setValue(strPortString.charAt(3) - '0');
		}
		if (mPort5 != null) {
			mPort5.setValue(strPortString.charAt(4) - '0');
		}
	}

	public void Invalidate() {
		mPort1.clearFocus();
		mPort2.clearFocus();
		mPort3.clearFocus();
		mPort4.clearFocus();
		mPort5.clearFocus();
	}

	public int getPort() {
		if (null == mPort1 || null == mPort2 || null == mPort3
				|| null == mPort4) {
			return -1;
		}
		String strPortString = "" + mPort1.getValue() + mPort2.getValue()
				+ mPort3.getValue() + mPort4.getValue() + mPort5.getValue();
		return Integer.parseInt(strPortString);
	}

	private void init() {
		//setCancelable(false);
		View view = mLayoutInflater.inflate(R.layout.portlayout, null);
		mPort1 = (NumberPicker) view.findViewById(R.id.numberPickerPort1);
		mPort1.setMinValue(0);
		mPort1.setMaxValue(9);
		mPort1.setValue(3);
		mPort2 = (NumberPicker) view.findViewById(R.id.numberPickerPort2);
		mPort2.setMinValue(0);
		mPort2.setMaxValue(9);
		mPort2.setValue(0);
		mPort3 = (NumberPicker) view.findViewById(R.id.numberPickerPort3);
		mPort3.setMinValue(0);
		mPort3.setMaxValue(9);
		mPort3.setValue(0);
		mPort4 = (NumberPicker) view.findViewById(R.id.numberPickerPort4);
		mPort4.setMinValue(0);
		mPort4.setMaxValue(9);
		mPort4.setValue(0);
		mPort5 = (NumberPicker) view.findViewById(R.id.numberPickerPort5);
		mPort5.setMinValue(0);
		mPort5.setMaxValue(9);
		mPort5.setValue(0);
		setTitle("�˿�");
		setView(view);
	}
}
